<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
use app\dboard\controller\User;
class Course extends Base
{
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
    }
    public function add_course(){
        $classes = Db::name('classes')
              ->field('id,name')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        $classroom = Db::name('classroom')
              ->field('id,name')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        $this->assign('classes_list',$classes);
        $this->assign('classroom_list',$classroom);
        return view('course/add_course');
    }
    public function edit_course(){
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $classes = Db::name('classes')
              ->field('id,name')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        $classroom = Db::name('classroom')
              ->field('id,name')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        $where = [];
        $where[] = ['id','=',$id];
        $info = Db::name('course')
                ->where($where)
                ->find();
        if ($info) {
          $info['week'] = '';
          $info['start_time'] = '';
          $info['end_time'] = '';
          $info['week_arr'] = [];
          $info['start_time_arr'] = [];
          $info['end_time_arr'] = [];
          if ($info['class_time']) {
              $class_time_arr = explode('|', $info['class_time']);
              foreach ($class_time_arr as $key => $value) {
                if ($key>0) {
                    $info['week'] .= '|';
                    $info['start_time'] .= '|';
                    $info['end_time'] .= '|';
                }
                $class_time_arr_arr = explode(',', $value);
                $info['week'] .= $class_time_arr_arr[0];
                $info['start_time'] .= $class_time_arr_arr[1];
                $info['end_time'] .= $class_time_arr_arr[2];
                $info['week_arr'][] = $class_time_arr_arr[0];
                $info['start_time_arr'][] = $class_time_arr_arr[1];
                $info['end_time_arr'][] = $class_time_arr_arr[2];
              }
          } else {
            $info['week_arr'] = [0];
            $info['start_time_arr'] = [''];
            $info['end_time_arr'] = [''];
          }
        }
        $this->assign('cid',$id);
        $this->assign('info',$info);
        $this->assign('classes_list',$classes);
        $this->assign('classroom_list',$classroom);
        return view('course/edit_course');
    }
    public function lists(){
        return view('course/lists');
    }
   public function ajaxList(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        $sid = isset($input['sid'])?$input['sid']:0;
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $where = [];
        $where[] = ['sid','=',$sid];
        $where[] = ['status','=',1];
        $field = '*';
        $count=Db::name('course')->where($where)->count();
        $list  = Db::name('course')
              ->field($field)
              ->where($where)
              ->order('id desc')
              ->limit($page_start, $page_limit)->select();
        $User = new User();
        $weekday = $User->weekday;
        foreach($list as $k=>&$v){
          $v['cover'] = get_newpic_url($v['pic']);
          $v['class_time_str'] = '';
          if ($v['class_time']) {
              $arr = explode('|', $v['class_time']);
              if ($arr) {
                  foreach ($arr as $key => $val) {
                      if ($key>0) $v['class_time_str'] .= '，';
                      $arr_arr = explode(',', $val);
                      if (count($arr_arr)==3) $v['class_time_str'] .= '每周'.$weekday[$arr_arr[0]].' '.$arr_arr[1].'-'.$arr_arr[2];
                  }
              }
          }
        }
        
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['course'=>$list,'Data'=>$list,'recordsTotal'=>$count];
        return $this->print_result($this->res_data);
   }
    public function infoData(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $where = [];
        $where[] = ['id','=',$id];
        $info = Db::name('course')
                ->where($where)
                ->find();
        if($info['pic']){
          $info['cover'] = get_newpic_url($info['pic']);
          $info['master_teacher_name'] = Db::name('teacher')->where('id',$info['teacher_id'])->value('name');
          $info['week'] = '';
          $info['start_time'] = '';
          $info['end_time'] = '';
          $info['week_arr'] = [];
          $info['start_time_arr'] = [];
          $info['end_time_arr'] = [];
          $class_time_arr = explode('|', $info['class_time']);
          foreach ($class_time_arr as $key => $value) {
            if ($key>0) {
                $info['week'] .= '|';
                $info['start_time'] .= '|';
                $info['end_time'] .= '|';
            }
            $class_time_arr_arr = explode(',', $value);
            if (count($class_time_arr_arr)==3) {
              $info['week'] .= $class_time_arr_arr[0];
              $info['start_time'] .= $class_time_arr_arr[1];
              $info['end_time'] .= $class_time_arr_arr[2];
              $info['week_arr'][] = $class_time_arr_arr[0];
              $info['start_time_arr'][] = $class_time_arr_arr[1];
              $info['end_time_arr'][] = $class_time_arr_arr[2];
            }
          }
        }

        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['course'=>$info];
        return $this->print_result($this->res_data);
   }
   public function doCreateCourse(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $param    = $this->request->param(false);
        $course = Controller('Course', 'logic');
        $result            = $course->create($param);
        return $this->print_result($result);
   }
   public function editCourseinfo(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $param    = $this->request->param(false);
        $course= Controller('Course', 'logic');
        $result            = $course->edit($param);
        return $this->print_result($result);
   }
   public function updateCourse(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $param    = $this->request->param(false);
        $course = Controller('Course', 'logic');
        $result            = $course->updateCourse($param);
        return $this->print_result($result);
   }
   public function getCourseInfo(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $param    = $this->request->param(false);
        $course_id = isset($param['course_id'])?$param['course_id']:0;
        if (!$course_id) {
            $this->res_data['Msg'] = '缺少参数';
            return $this->print_result($this->res_data);
        }
        $User = new User();
        $weekday = $User->weekday;
        $course = Db::name('course')
              ->field('*')
              ->where(['id'=>$course_id])
              ->order('id desc')->find();
        if (!$course) {
            $this->res_data['Msg'] = '课程不存在';
            return $this->print_result($this->res_data);
        }
        $course['class_time_str'] = '';
        if ($course['class_time']) {
            $arr = explode('|', $course['class_time']);
            if ($arr) {
                foreach ($arr as $k => $v) {
                    if ($k>0) $course['class_time_str'] .= '，';
                    $arr_arr = explode(',', $v);
                    if (count($arr_arr)==3) $course['class_time_str'] .= '每周'.$weekday[$arr_arr[0]].' '.$arr_arr[1].'-'.$arr_arr[2];
                }
            }
        }
        $students = Db::name('user_course')->alias('uc')->leftJoin('user u','uc.user_id=u.id')->where('uc.course_id='.$course_id.' and uc.status in (1,2)')->field('u.photo,u.name')->select();
        if ($students) {
          foreach ($students as $kk => &$vv) {
            $vv['photo'] = config('upload_domain').$vv['photo'];
          }
        }
        $classtime = Db::name('classtime')->where(['status'=>1,'course_id'=>$course_id])->select();
        if ($classtime) {
          foreach ($classtime as $key => &$value) {
            $value['begin_time'] = date('Y/m/d H:i',$value['begin_time']);
            $value['end_time'] = date('Y/m/d H:i',$value['end_time']);
          }
        }
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['course'=>$course,'students'=>$students,'classtime'=>$classtime];
        return $this->print_result($this->res_data);
   }
}
